package com.ggh.jinjilive.ui.mine.zhubo;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.BarUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityLiushuiDetailsBinding;
import com.ggh.jinjilive.mobile.mine.zhubo.LiuShuiDetailsModel;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.mine.zhubo.adapter.LiuShuiDetailsAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Route(path = ARouterUtils.LiuShuiDetailsActivity)
public class LiuShuiDetailsActivity extends BaseActivity<ActivityLiushuiDetailsBinding> implements OnRefreshListener, OnLoadMoreListener {
    //VM
    private ZhuBoViewModel zhuBoViewModel;
    //适配器
    private LiuShuiDetailsAdapter liuShuiDetailsAdapter;
    //日期
    private String date;
    private int distinction;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        zhuBoViewModel = new ViewModelProvider(this,viewModelFactory).get(ZhuBoViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_liushui_details;
    }

    @Override
    public void initParam() {
        super.initParam();
        date = getIntent().getStringExtra("date");
        distinction = getIntent().getIntExtra("distinction",0);
    }

    @Override
    public void initView() {
        super.initView();
        setWhiteActionBarStyle("钻石流水",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);

        loadService = LoadSir.getDefault().register(binding.refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //加载
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
        //
        onRefresh(binding.refreshLayout);
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        //适配器
        liuShuiDetailsAdapter = new LiuShuiDetailsAdapter();
        binding.rvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvGoods.setAdapter(liuShuiDetailsAdapter);


    }

    @Override
    public void initData() {
        super.initData();

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
        //钻石流水
        zhuBoViewModel.mListLiuShuiDetailsModel.observe(this, new Observer<List<LiuShuiDetailsModel>>() {
            @Override
            public void onChanged(List<LiuShuiDetailsModel> liuShuiDetailsModels) {
                binding.refreshLayout.finishRefresh();
                if(zhuBoViewModel.isRefresh){
                    if(null != liuShuiDetailsModels && liuShuiDetailsModels.size() > 0){
                        loadService.showSuccess();
                        if(null != liuShuiDetailsAdapter){
                            liuShuiDetailsAdapter.setAdd(liuShuiDetailsModels);
                        }
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(null != liuShuiDetailsModels && liuShuiDetailsModels.size() > 0){
                        if(null != liuShuiDetailsAdapter){
                            liuShuiDetailsAdapter.add(liuShuiDetailsModels);
                        }
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        zhuBoViewModel.isRefresh = false;
        Map<String, Object> params = new HashMap<>();
        if(distinction == 0){
            params.put("day",date);
            params.put("type", "2");
        }else if(distinction == 1){
            params.put("week",date);
        }else if(distinction == 2){
            params.put("month",date);
            params.put("type", "3");
        }
        zhuBoViewModel.page = zhuBoViewModel.page + 1;
        params.put("page", zhuBoViewModel.page);
        //钻石流水
        zhuBoViewModel.AnchorWeekSilverDetail(params);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        zhuBoViewModel.isRefresh = true;
        Map<String, Object> params = new HashMap<>();
        if(distinction == 0){
            params.put("day",date);
            params.put("type", "2");
        }else if(distinction == 1){
            params.put("week",date);
        }else if(distinction == 2){
            params.put("month",date);
            params.put("type", "3");
        }
        zhuBoViewModel.page = 1;
        params.put("page", zhuBoViewModel.page);
        //钻石流水
        zhuBoViewModel.AnchorWeekSilverDetail(params);
    }
}
